/*
 *  Engine.cpp
 *  SypXIPhone
 *
 *  Created by Luv Khemani on 5/14/10.
 *  Copyright 2010 Luv Khemani. All rights reserved.
 *
 */

#include "Engine.h"
#include "Base.h"
#include "CanvasIPhoneGL.h"
#include "RendererGLES1.h"
#include "AudioEngineIPhone.h"


namespace SypX
{
	Canvas* Engine::createCanvas(UInt width, UInt height)
	{		
		return new CanvasIPhoneGL();		
	}
	
	Renderer* Engine::createRenderer()
	{
		return new RendererGLES1();		
	}
	
	//On iPhone we just call cavas->init() only, engine->init and userland init are called from within
	void Engine::initAndStart(UInt width, UInt height)
	{
		canvas = createCanvas(width, height);	
		static_cast<CanvasIPhoneGL*>(canvas)->init();//Control is lost from here, never returns until app is quit in iPhone
	}
	
	String Engine::getFilePath(const String& filename)
	{
		String ext = filename.substr( filename.length()-3,3 );
		String name = filename.substr( 0, filename.length()-4);
		name = name.substr(name.find_last_of("/")+1);
		
		NSString* extC = [NSString stringWithUTF8String:ext.c_str()];
		NSString* nameC = [NSString stringWithUTF8String:name.c_str()];
		NSString * path = [[NSBundle mainBundle] pathForResource:nameC   ofType:extC];
		String result( [path cStringUsingEncoding:1] );
		return result;
	}
	
	AudioEngine* Engine::createAudioEngine()
	{
		return new AudioEngineIPhone();
	}
}